package cn.luckdou.commons.utils;

import cn.luckdou.system.model.bus_dict.BusDict;
import cn.luckdou.system.service.bus_dict.IBusDictService;

import java.util.List;

/**
 * Created by Dadou on 2017/8/9.
 */
public class DictUtils {
    private static IBusDictService iBusDictService = SpringContextHolder.getBean(IBusDictService.class);

    /**
     * 获取指定类型的字典列表 option 格式
     *
     * @param type
     * @return
     */
    public static String getBusDictListToOption(String type) {
        StringBuffer str = new StringBuffer();
        if (StringUtils.isNotBlank(type)) {
            for (BusDict busDict : getList(type)) {
                str.append("<option value=\"" + busDict.getValue() + "\">" + busDict.getLabel() + "</option>\n                            ");
            }
            return str.toString();
        }
        return null;
    }

    /**
     * 获取指定类型的字典列表 case 格式
     *
     * @param type
     * @return
     */
    public static String getBusDictListToCase(String type) {
        StringBuffer str = new StringBuffer("formatter : function(value, row, index) {\n" +
                "\t                switch (value) {\n");
        if (StringUtils.isNotBlank(type)) {
            for (BusDict busDict : getList(type)) {
                str.append("\t                case " + busDict.getValue() + ":\n");
                str.append("\t                    return '" + busDict.getLabel() + "';\n");
            }
            str.append("\t                }\n");
            str.append("\t            }");
            return str.toString();
        }
        return null;
    }


    /**
     * 根据 type 和 VALUE 获取 label
     *
     * @param type
     * @param value
     * @return
     */
    public static String getBusDicLabel(String type, String value, String defaultLabel) {
        if (StringUtils.isNotBlank(type)) {
            for (BusDict busDict : getList(type)) {
                if (busDict.getValue().toString().equals(value))
                    return busDict.getLabel();
            }
        }
        return defaultLabel;
    }

    /**
     * TODO 后期加缓存  字典列表
     *
     * @param type
     * @return
     */
    public static List<BusDict> getList(String type) {
        if (StringUtils.isNotBlank(type)) {
            JedisUtil.Strings strings = JedisUtil.getInstance().new Strings();
            byte[] bytes =null;
            try{
                bytes = strings.get(("bus_dict_" + type).getBytes());
            }catch (Exception e){
            }
            if (bytes != null) {
                return (List<BusDict>) SerializeUtil.unserialize(bytes);
            } else {
                List<BusDict> listByType = iBusDictService.getListByType(type);
                try{
                    String set = strings.set("bus_dict_" + type, SerializeUtil.serialize(listByType));
                }catch (Exception e){
                }
                return listByType;
            }
        }
        return null;
    }

    /**
     * 重置redis中的值
     * @param type
     * @return
     */
    public static boolean resetBusDictList(String type){
        if (StringUtils.isNotBlank(type)) {
            try{
                List<BusDict> listByType = iBusDictService.getListByType(type);
                JedisUtil.Strings strings = JedisUtil.getInstance().new Strings();
                String set = strings.set("bus_dict_" + type, SerializeUtil.serialize(listByType));
                return true;
            }catch (Exception e){
                return false;
            }
        }
        return false;
    }



}